import { Component, OnInit, Input } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from "@angular/forms"
import { NzModalRef, NzModalService } from 'ng-zorro-antd/modal';

import { DictionaryManageService } from "../dictionary-manage.service"
import { HCommonService } from 'src/app/h-common/h-common.service';

@Component({
    selector: 'app-dictionary-manage-detail',
    templateUrl: './dictionary-manage-detail.component.html',
    styleUrls: ['./dictionary-manage-detail.component.scss']
})
export class DictionaryManageDetailComponent implements OnInit {
    private deleteModal: NzModalRef;
    public detailData = [];
    public detailCount: Number;
    public detailPage = <Number>1;
    detailParentName = <string>"";
    private detailParentId = <number>-1;
    searchKey = <string>"";
    private detailId = <number>-1;
    pageSize : number = 10;

    public isDetailOkLoading = <Boolean>false;
    public isDetailVisible = <Boolean>false;
    public detailValidateForm: FormGroup;
    public detailLabel = <string>"";
    public detailValue = <string>"";
    public detailSort = <number>0;
    bornRole = {}

    modalDomBox : any

    constructor(
        private dictionaryManageService: DictionaryManageService,
        private fb: FormBuilder,
        private modal: NzModalService,
        private commonService : HCommonService
    ) { }

    ngOnInit() {
        this.modalDomBox = document.querySelector("#modal_box_2");
        this.bornRole = this.commonService.getRoles("dict");

        this.detailValidateForm = this.fb.group({
            detailLabel: [null, [Validators.required]],
            detailValue: [null, [Validators.required]],
            detailSort: [null, [Validators.required]]
        })
    }

    ngDoCheck() {
        if (this.detailParentName !== this.dictionaryManageService.detailParantName) {
            this.detailParentName = this.dictionaryManageService.detailParantName;
            this.detailParentId = this.dictionaryManageService.detailParentId;
            this.getDetail();
        }
    }
    hotSearch(){
        this.detailPage = 1;
        this.getDetail();
    }
    getDetail() {
        let _dataJson = {
            label: this.searchKey,
            dictName: this.detailParentName,
            page: this.detailPage,
            size: 10,
            // sort:"sort asc"
        }
        this.dictionaryManageService.getDetail(_dataJson).subscribe(res => {
            this.detailData = res.content;
            this.detailCount = res.totalElements;
        })
    }
    handleShowDetailModal() {
        this.detailValidateForm.reset();
        this.isDetailVisible = true;
        this.detailId = -1;
    }
    handleDetailCancel(): void {
        this.isDetailVisible = false;
        this.isDetailOkLoading = false;
    }
    handleDetailSubmit() {
        if (this.detailId == -1) {
            this.detailAdd();
        }else{
            this.edit();
        }
    }
    handleDeleteModal(id) {
        this.deleteModal = this.modal.confirm({
            nzTitle: '删除提示',
            nzContent: '确定删除这条数据吗？',
            nzOnOk: () =>
                new Promise((resolve, reject) => {
                    this.delete(id)
                }).catch(() => console.log('删除失败'))
        })
    }
    handleSearch() {
        this.detailPage = 1;
        this.getDetail();
    }
    handleEdit(data) {
        this.isDetailVisible = true;
        this.detailId = data.id;
        this.detailLabel = data.label;
        this.detailValue = data.value;
        this.detailSort = data.sort;
    }
    detailAdd() {
        for (const i in this.detailValidateForm.controls) {
            this.detailValidateForm.controls[i].markAsDirty();
            this.detailValidateForm.controls[i].updateValueAndValidity();
        }

        if (this.detailValidateForm.status == "VALID") {
            this.isDetailOkLoading = true;
            let _value = this.detailValidateForm.value;
            let _dataJson = {
                dict: {
                    id: this.detailParentId
                },
                dictName: this.detailParentName,
                label: _value.detailLabel,
                value: _value.detailValue,
                sort: _value.detailSort
            }
            this.dictionaryManageService.detailAdd(_dataJson).subscribe(res => {
                this.getDetail();
                this.handleDetailCancel();
            }, (data)=>{
                this.isDetailOkLoading = false;
            })
        }
    }
    delete(id) {
        this.dictionaryManageService.apiDelete(id).subscribe(res => {
            this.getDetail();
            this.deleteModal.close();
        })
    }
    pageChange(ev) {
        this.detailPage = ev;
        this.getDetail();
    }
    edit(){
        this.isDetailOkLoading = true;
        let _value = this.detailValidateForm.value;
        let _dataJson = {
            dict: {
                id: this.detailParentId
            },
            dictName: this.detailParentName,
            id : this.detailId,
            label: _value.detailLabel,
            value: _value.detailValue,
            sort: _value.detailSort
        }
        this.dictionaryManageService.apiEdit(_dataJson).subscribe(res=>{
            this.getDetail();
            this.handleDetailCancel();
        })
    }
}
